package com.movie.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.movie.beans.MemberBeans;
import com.movie.dao.MemberDao;

public class MemberAction implements Action{
	int type;
	public MemberAction(int type){
		this.type=type;
		
	}
	MemberBeans member = new MemberBeans();
	MemberDao dao = new MemberDao();
	public Forward execute(HttpServletRequest request, HttpServletResponse response){
		Forward forward = new Forward();
		if(type==0){
			forward = loginMember(request);
			
		}else if(type==1){
			member = MemberInsert(request,member);
			
			forward.setPagePath("login.jsp");	//login했을 때의 페이지 경로 설정(servlet호출)
			forward.setRedirect(false);				//main페이지 읽어오기 전 서블릿을 먼저 실행하므로 redirect=false
			
		}
		return forward;
	}
		
	
		
		
	
	private void idcheck(){
		
	}
	private MemberBeans MemberInsert(HttpServletRequest request, MemberBeans member){
		member.setId(request.getParameter("id"));
		member.setPwd(request.getParameter("pw"));
		member.setName(request.getParameter("name"));
		member.setPhone(request.getParameter("phone"));
		member.setGender(request.getParameter("gender"));
		member.setEmail(request.getParameter("email"));
		member.setImage(request.getParameter("image"));
		
		boolean check = dao.MemberInsert(member);
		
		
		return member;
	}
	private void searchId(){
	
	}
	private void searchPwd(){
		
	}
	private void modifyPwd(){
		
	}
	private Forward loginMember(HttpServletRequest request){
		Forward forward = new Forward(); //login성공 여부에 따라 경로 지정, isRedirect 여부를 정해주기 위해 forward호출
		
		HttpSession session = request.getSession(); //login 성공 시 session 생성(값을 저장해줘야 session생성완료된것임)
		MemberBeans member = new MemberBeans();
		
		member.setId(request.getParameter("id"));
		member.setPwd(request.getParameter("pw"));
				
		boolean check = dao.loginMember(member);
		
		
		if(check){
			//when succeed loin
			session.setAttribute("id", member.getId());	//memory영역에 id 넣어줌
			session.setAttribute("member", member);
						
			forward.setPagePath("main.zzang");	//login했을 때의 페이지 경로 설정(servlet호출)
			forward.setRedirect(false);				//main페이지 읽어오기 전 서블릿을 먼저 실행하므로 redirect=false
		}else{
			forward.setPagePath("./login.jsp");	//login실패했을 때의 페이지 경로 설정
			forward.setRedirect(true);		//넘길 data가 없으니 redirect방식으로
		}
		
		return forward;
	}
	private void MemberInfo(){
		
	}
	private void ModifyMember(){
		
	}
	private void DeleteMember(){
		
	}

}
/*StringBuffer sb = new StringBuffer();
sb.append("<table width='150px'>");
for(int i=0;i<m1.size();i++){
	sb.append("<tr>*"+m1.get(i).getBOOKS()+"<br></tr>");	
}
sb.append("</table>");
session.setAttribute("rent", sb.toString());

StringBuffer sb1 = new StringBuffer();
sb.append("<table>");
for(int i=0;i<m2.size();i++){
	sb1.append("<tr>*"+m2.get(i).getBOOKS()+"<br></tr>");	
}
sb.append("</table>");
session.setAttribute("delay", sb1.toString());

StringBuffer sb2 = new StringBuffer();
sb.append("<table>");
for(int i=0;i<m3.size();i++){
	sb2.append("<tr>*"+m3.get(i).getBOOKS()+"<br></tr>");	
}
sb.append("</table>");
session.setAttribute("booking", sb2.toString());*/
